package com.adinnet.admin.system.controller;

import com.adinnet.admin.common.service.CommonService;
import com.adinnet.admin.system.model.SysMenu;
import com.adinnet.admin.system.service.SysMenuService;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author sunjianbo
 * @since 2019-05-17
 */
@Controller
@RequestMapping("/sys/menu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private CommonService commonService;

    @RequiresPermissions("menu:view")
    @RequestMapping()
    public String authorities(Model model) {
        List<SysMenu> authorities = sysMenuService.list(new QueryWrapper<SysMenu>().eq("is_menu", 0).orderByAsc("order_number"));
        model.addAttribute("authorities", authorities);
        return "system/menu.html";
    }

    /**
     * 查询所有菜单
     **/
    @RequiresPermissions("menu:view")
    @ResponseBody
    @RequestMapping("/list")
    public PageResult<SysMenu> list() {
        List<SysMenu> authorities = sysMenuService.list(new QueryWrapper<SysMenu>().orderByAsc("order_number"));
        return new PageResult<>(authorities);
    }

    /**
     * 添加菜单
     */
    @RequiresPermissions("menu:update")
    @ResponseBody
    @RequestMapping("/add")
    public JsonResult add(SysMenu authorities, HttpServletRequest request) {
        if (sysMenuService.save(authorities)) {
            commonService.saveOperation(request,"菜单管理-添加");
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    /**
     * 修改菜单
     */
    @RequiresPermissions("menu:update")
    @ResponseBody
    @RequestMapping("/update")
    public JsonResult update(SysMenu authorities, HttpServletRequest request) {
        if (sysMenuService.updateById(authorities)) {
            commonService.saveOperation(request,"菜单管理-修改");
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    /**
     * 删除菜单
     */
    @RequiresPermissions("menu:update")
    @ResponseBody
    @RequestMapping("/delete")
    public JsonResult delete(Integer authorityId, HttpServletRequest request) {
        SysMenu sysMenu=new SysMenu();
        sysMenu.setId(authorityId);
        sysMenu.setDeleted(true);
        if (sysMenuService.updateById(sysMenu)) {
            commonService.saveOperation(request,"菜单管理-删除");
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }
}
